package cn.wangliang181230.boot.web.environmentpostprocessor;

import cn.wangliang181230.boot.web.propertysource.MyFunctionPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

public class MyFunctionPropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

	@Override
	public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
		// 创建配置源
		MyFunctionPropertySource mfps = new MyFunctionPropertySource("my-function");

		// 放入配置源列表中
		String name1 = "systemEnvironment";
		String name2 = "systemProperties";
		if (environment.getPropertySources().get(name1) != null) {
			environment.getPropertySources().addAfter(name1, mfps);
		} else if (environment.getPropertySources().get(name2) != null) {
			environment.getPropertySources().addAfter(name2, mfps);
		} else {
			environment.getPropertySources().addLast(mfps);
		}
	}


	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE + 1; // 函数式配置源，优先级可以高一些，因为它不依赖于其他配置。
	}

}
